import { FairyGUI, FairyEditor, System } from 'csharp';
import ListItem from './ListItem';
/**
 * 封装FairyGUI.GList
 * @author clong 2020.11.01
 */
export default class EGlist extends FairyGUI.EventDispatcher {

    public name:string = "EGlist";Fz

    protected list: FairyGUI.GList;
    protected _array: Array<any>;
    protected _thisObject: any;
    protected _itemRenderer: Function = null;

    protected clickItemCallBack: FairyGUI.EventCallback1 = null;
    protected scrollEndCallBack: FairyGUI.EventCallback1 = null;
    /**上一次选择索引 */
    private _preSelectedIndex: number = -1;

    private stageMouseDown:boolean = false;

    constructor(list: FairyGUI.GList, thisObject: any = null) {

        super();

        this.list = list;
        this._thisObject = thisObject;
        this._array = [];
        if (this.list != null) {
            this._thisObject = thisObject || this;
            // this.list.itemRenderer = new Laya.Handler(this, this.listItemRender, null, false);
            // this.list.on(fgui.Events.CLICK_ITEM, this, this.clickItem);
            // this.list.on(fgui.Events.SCROLL_END, this, this.listScrollEnd);
            this.list["$eglist"] = this;

            this.list.itemRenderer = (index: number, obj: FairyGUI.GObject)=>{ this.listItemRender(index,obj) }; //这里可以了

            this.clickItemCallBack = (context: FairyGUI.EventContext) => {
                this.clickItem(context.data as FairyGUI.GButton);
            }
            this.list.onClickItem.Add(this.clickItemCallBack);

            this.scrollEndCallBack = (context: FairyGUI.EventContext) => {
                this.listScrollEnd();
            }
            this.list.scrollPane.onScrollEnd.Add( this.scrollEndCallBack );
        }
    }

    /**
     * 渲染条目
     */
    protected listItemRender(index: number, obj: FairyGUI.GObject): void {

        // console.log("------------->listItemRender: "+ index + " obj: " + obj + " data: " + (this._array && this._array[index]) );
        if (obj instanceof ListItem) {            
            obj.setIndex(index);
            obj.name = this.name + "_" + index;
            obj.data = this._array && this._array[index];
        }else if( obj instanceof FairyGUI.GButton ){
            obj.title = this._array && this._array[index];
        }
    }

    private clickItem(target: any): void {

        console.log( "[EGlist.clickItem]---------->" + target );
        if (this.isSingle && this._preSelectedIndex != this.list.selectedIndex) {
            this.selectItem(target);//单选tab模式
        } else if (!this.isSingle) {
            this.selectItem(target);
        }
        this._preSelectedIndex = this.list.selectedIndex;
    }

    public set selectedIndex( value:number ){

        this.list.selectedIndex = value;
        
        let item = value >= 0 && this.list.GetChildAt(value);
        if( item ){
            this.clickItem( item );
        }
    }

    public get selectedIndex():number{

        return this.list.selectedIndex;
    }

    /**选择条目 */
    private selectItem(item: any): void {

        // this.event(FairyGUI.Events.CLICK_ITEM, item);

        this.DispatchEvent(FairyEditor.FEvents.CLICK_ITEM, item);
    }

    public set array(value: Array<any>) {

        this.list.RemoveChildren();
        this._array = value || [];
        this.list.numItems = this._array.length;
    }

    public get array(): Array<any> {

        return this._array;
    }

    public get numChildren():number{

        return this.list.numChildren;
    }

    public getChildAt(i:number):any{

        return this.list.GetChildAt(i);
    }

    /**列表是否为单选模式 */
    public get isSingle(): boolean {

        return this.list.selectionMode == FairyGUI.ListSelectionMode.Single;
    }

    /**
     * 取消选择
     */
    public canelSelect(): void {

        this._preSelectedIndex = -1;
        this.list.ClearSelection();
    }
    private mouseUp(): void {

        this.stageMouseDown = false;
    }

    private listScrollEnd(): void {

        this.mouseUp();
    }
}